package generator.website;

import java.util.Iterator;
import org.eclipse.emf.common.util.EList;
import website.*;

public class WebSiteStrutsConfigFactory
{
  protected static String nl;
  public static synchronized WebSiteStrutsConfigFactory create(String lineSeparator)
  {
    nl = lineSeparator;
    WebSiteStrutsConfigFactory result = new WebSiteStrutsConfigFactory();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + NL + "<!DOCTYPE struts-config PUBLIC" + NL + "\"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN\"" + NL + "\"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd\">" + NL + "<struts-config>" + NL + "<form-beans>";
  protected final String TEXT_2 = NL + "\t\t<form-bean name=\"";
  protected final String TEXT_3 = "ValidationForm\" type=\"actionForms.";
  protected final String TEXT_4 = "ValidationForm\"/>";
  protected final String TEXT_5 = NL + "</form-beans>" + NL + "<action-mappings>";
  protected final String TEXT_6 = "\t\t\t\t" + NL + "\t\t\t\t<action path=\"/pages/";
  protected final String TEXT_7 = "\" forward=\"/pages/";
  protected final String TEXT_8 = ".jsp\" />";
  protected final String TEXT_9 = NL + "\t\t<action path=\"/";
  protected final String TEXT_10 = "\" type=\"servlets.";
  protected final String TEXT_11 = "Action\" name=\"";
  protected final String TEXT_12 = "ValidationForm\" scope=\"request\" input=\"/pages/";
  protected final String TEXT_13 = ".jsp\">" + NL + "\t\t<forward name=\"success\" path=\"/pages/";
  protected final String TEXT_14 = ".jsp\" />" + NL + "\t\t<forward name=\"error\" path=\"/pages/";
  protected final String TEXT_15 = ".jsp\" />" + NL + "\t\t</action>";
  protected final String TEXT_16 = "\t\t\t\t" + NL + "\t\t\t<action path=\"/pages/";
  protected final String TEXT_17 = "\" forward=\"/pages/";
  protected final String TEXT_18 = ".jsp\" />";
  protected final String TEXT_19 = NL + "</action-mappings>" + NL + "<!-- ========== message file =========================== -->" + NL + "<message-resources parameter=\"Resources\" />" + NL + "</struts-config>";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    WebSite webSite = (WebSite) argument;
    stringBuffer.append(TEXT_1);
    EList<Page> pages = webSite.getPages();
for(Page page : pages){
	EList<Form> forms = page.getForms();
	for(Form form : forms){
		
    stringBuffer.append(TEXT_2);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_3);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_4);
    
	}
}

    stringBuffer.append(TEXT_5);
    for(Page page : pages){
	EList<Form> forms = page.getForms();
	for(Form form : forms){
		EList<Tag> tags = form.getTags();
		for(Tag tag : tags)
			if(tag instanceof Link){
				Link linkTag = (Link)tag;
				
    stringBuffer.append(TEXT_6);
    stringBuffer.append(linkTag.getName());
    stringBuffer.append(TEXT_7);
    stringBuffer.append(linkTag.getTarget().getName());
    stringBuffer.append(TEXT_8);
    
			}
		
    stringBuffer.append(TEXT_9);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_10);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_11);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_12);
    stringBuffer.append(page.getName());
    stringBuffer.append(TEXT_13);
    stringBuffer.append(form.getSuccessPage().getName());
    stringBuffer.append(TEXT_14);
    stringBuffer.append(form.getErrorPage().getName());
    stringBuffer.append(TEXT_15);
    
	}
	EList<PageTag> tags = page.getPageTags();
	for(Tag tag : tags)
		if(tag instanceof Link){
			Link linkTag = (Link)tag;
			
    stringBuffer.append(TEXT_16);
    stringBuffer.append(linkTag.getName());
    stringBuffer.append(TEXT_17);
    stringBuffer.append(linkTag.getTarget().getName());
    stringBuffer.append(TEXT_18);
    
		}
}

    stringBuffer.append(TEXT_19);
    return stringBuffer.toString();
  }
}
